import 'package:flutter/material.dart';
import 'package:flutter_learning/bluetooth/ly_ble_manager.dart';
import 'package:flutter_learning/isolate/utils/ly_log.dart';
import 'package:flutter_learning/polar/polar_manager.dart';
import 'package:flutter_learning/utils/ly_app_color.dart';
import 'package:vector_math/vector_math_64.dart';

class LYBluetoothView extends StatefulWidget {
  const LYBluetoothView({super.key});

  @override
  State<LYBluetoothView> createState() => _LYBluetoothViewState();
}

class _LYBluetoothViewState extends State<LYBluetoothView> {
  @override
  void initState() {
    super.initState();

    // final manager = PolarManager();
    // manager.connect();
    LYBLEManager.share.init((result) {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("蓝牙"),
        centerTitle: true,
      ),
      body: ListView.separated(
          itemBuilder: (ctx, index) {
            final item = LYBLEManager.share.devices[index];
            return GestureDetector(
              onTap: () {
                LYLog.d("==============");
                LYBLEManager.share.connectDevice(item);
              },
              child: Container(
                alignment: Alignment.center,
                height: 50,
                child: Text(
                  item.advertisementData.advName,
                  style: const TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 15,
                    color: AppColor.blackColor,
                  ),
                ),
              ),
            );
          },
          separatorBuilder: (ctx, index) {
            return const Divider(
              height: 1,
            );
          },
          itemCount: LYBLEManager.share.devices.length),
      floatingActionButton: TextButton(
        onPressed: () {
          LYBLEManager.share.scanBlueDevice();
        },
        child: const Text("刷新"),
      ),
    );
  }
}
